package com.youlai.boot.bmp.service.impl;

import com.aizuda.bpm.engine.entity.FlwProcess;
import com.youlai.boot.bmp.model.query.FlwProcessQuery;
import com.youlai.boot.bmp.model.vo.BizProcessVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.bmp.mapper.FormWorkflowBindingMapper;
import com.youlai.boot.bmp.service.FormWorkflowBindingService;
import com.youlai.boot.bmp.model.entity.FormWorkflowBinding;
import com.youlai.boot.bmp.model.form.FormWorkflowBindingForm;
import com.youlai.boot.bmp.model.query.FormWorkflowBindingQuery;
import com.youlai.boot.bmp.model.vo.FormWorkflowBindingVO;
import com.youlai.boot.bmp.converter.FormWorkflowBindingConverter;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.lang.Assert;

/**
 * 单与工作流绑定服务实现类
 *
 * @author rabit
 * @since 2025-11-04 16:22
 */
@Service
@RequiredArgsConstructor
public class FormWorkflowBindingServiceImpl extends ServiceImpl<FormWorkflowBindingMapper, FormWorkflowBinding> implements FormWorkflowBindingService {

    private final FormWorkflowBindingConverter formWorkflowBindingConverter;

    /**
    * 获取单与工作流绑定分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<FormWorkflowBindingVO>} 单与工作流绑定分页列表
    */
    @Override
    public IPage<FormWorkflowBindingVO> getFormWorkflowBindingPage(FormWorkflowBindingQuery queryParams) {
        return this.baseMapper.getFormWorkflowBindingPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
    }

    /**
     * 获取单与工作流绑定表单数据
     *
     * @param id 单与工作流绑定ID
     * @return 单与工作流绑定表单数据
     */
    @Override
    public FormWorkflowBindingForm getFormWorkflowBindingFormData(Long id) {
        FormWorkflowBinding entity = this.getById(id);
        return formWorkflowBindingConverter.toForm(entity);
    }

    /**
     * 新增单与工作流绑定
     *
     * @param formData 单与工作流绑定表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveFormWorkflowBinding(FormWorkflowBindingForm formData) {
        FormWorkflowBinding entity = formWorkflowBindingConverter.toEntity(formData);
        return this.save(entity);
    }

    /**
     * 更新单与工作流绑定
     *
     * @param id   单与工作流绑定ID
     * @param formData 单与工作流绑定表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateFormWorkflowBinding(Long id,FormWorkflowBindingForm formData) {
        FormWorkflowBinding entity = formWorkflowBindingConverter.toEntity(formData);
        return this.updateById(entity);
    }

    /**
     * 删除单与工作流绑定
     *
     * @param ids 单与工作流绑定ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteFormWorkflowBindings(String ids) {
        Assert.isTrue(StringUtils.isNotBlank(ids), "删除的单与工作流绑定数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return removeByIds(idList);
    }


    public List<BizProcessVo> getBizProcessVoList(FlwProcessQuery query){
        return this.baseMapper.geBizProcessVoList(query);
    }


    public BizProcessVo getBizProcessVoById(Long id){
        return this.baseMapper.geBizProcessVoById(id);
    }

}
